/**
 * 
 */
package com.ideeli.webextractor.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.ideeli.webextractor.dao.ProductDao;
import com.ideeli.webextractor.utils.FileHelper;

/**
 * @author sa
 *
 */
public class SEOServiceImpl implements SEOService
{
	private static final Logger logger = Logger.getLogger(SEOServiceImpl.class);
	
	private ProductDao productDao;
	
	private static final String header="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
										+"<urlset" 
										+"\nxmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"" 
										+"\nxmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" 
										+"\nxsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9" 
										+"\nhttp://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\""
										+"\n>\n";
	
	private String url="<url>"
			+ "\n\t<loc>http://itshop.com.sg/index.php?route=product/product&#x26;product_id=replaceId</loc>"
			+ "\n</url>\n";


	
	@Override
	public String generateSiteMap() throws Exception 
	{
		logger.info("start  generateSiteMap");
		
		StringBuilder siteMapBuilder=new StringBuilder(header);
		
		List<Long> prodIds=productDao.getActiveProdIds();
		
		for(Long prodId:prodIds)
		{
			logger.info(prodId);
			String prod=url.replaceAll("replaceId", prodId.toString());
			siteMapBuilder.append(prod);
		}
		
		siteMapBuilder.append("\n</urlset>");
		
		String siteMap=siteMapBuilder.toString();
		
		createFile(siteMap);
		
		
		logger.info("Finish  generateSiteMap");
		return siteMap;
	}
	
	
	private void createFile(String siteMap) {
		
		try {
			FileHelper.writeFile("S:\\data\\sitemaps\\itshopMap_sitemap.xml", siteMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}


	public ProductDao getProductDao() {
		return productDao;
	}

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}
	

}
